//
//  SortViewController.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/10/31.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "SortViewController.h"
#import "AlbumCell.h"
#import "SortDetailViewController.h"


@interface SortViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *sortMainTableView;

@property (strong, nonatomic) NetworkHelper *networkHelper;

@property (strong, nonatomic) SortDetailViewController *sortDetailVC;

@end

@implementation SortViewController


- (NetworkHelper *)networkHelper {
    if (!_networkHelper) {
        _networkHelper = [NetworkHelper sharedInstance];
    }
    return _networkHelper;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self funNetwork];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData];
}



- (void) funNetwork {
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status <= 0) {
            NSLog(@"WTF");
        }
        else {
            NSLog(@"有网了");
            [self loadData];
              }
    }];
}




- (void) loadData {
    
    [self.sortMainTableView registerNib:[UINib nibWithNibName:@"AlbumCell" bundle:nil] forCellReuseIdentifier:@"sortflag"];
    
//    [self.networkHelper downloadWithURL:k_AblumURL WithRequestAllDataDidFinish:^{
//        
//        [self.sortMainTableView reloadData];
//        //NSLog(@"%ld", self.networkHelper.allAlbumArray.count);
//    }];
    
    [self.networkHelper downloadWithURL:k_AblumURL WithRequestAllDataDidFinish:^(BOOL status) {
        if (status) {
            [self.sortMainTableView reloadData];

        } else {
            NSLog(@"没有网络(专辑打印)");
        }
    }];
    
}



- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.networkHelper.allAlbumArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sortflag" forIndexPath:indexPath];
    
    AlbumModel *model = self.networkHelper.allAlbumArray[indexPath.row];
    
    //NSLog(@"%@", model);
    
    cell.albumModel = model;
    UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView = backView;
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    return cell;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if ([NetworkHelper judgeNetworkStatus]) {
        
        self.sortDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"SortDetailVC"];
        
        self.sortDetailVC.reciveModel = self.networkHelper.allAlbumArray[indexPath.row];
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        [tempAppDelegate.window.rootViewController presentViewController:self.sortDetailVC animated:YES completion:nil];
    }

    
}


- (BOOL)shouldAutorotate {
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}



@end
